﻿using DevExpress.XtraReports.UI;
using DevicesView.Logic.App;
using System.Collections.Generic;
using System.Text;
using BCL.Tool.Extend;
using System.Drawing;
using XiaonuanPMS.Logic;
using XiaonuanPMS.Entity;
using System;

namespace DevicesView.Print
{
    public partial class LabReportA4 : XtraReport
    {

        private Devices.Result result;

        private TestingsLogic logic;
        private RPResult rPBreedDatas;



        //LabResultVO entity;

        public LabReportA4(List<Devices.Result> _result)
        {
            InitializeComponent();

            result = _result?[0];
            this.PaperKind = System.Drawing.Printing.PaperKind.A4;
            if (AppResource.CurrentResource.Hospital != null)
                this.xrPictureBox3.Image = AppResource.CurrentResource.Hospital.Logo?.ToImage();
        }

        private void LabReportA4_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {

            //HqLabResultLogic logic = new HqLabResultLogic();
            //HqLabResult entity = logic.GetLabResult(orgId, regid, reportId);

            logic = new XiaonuanPMS.Logic.TestingsLogic();

            if (result != null)
            {
                Devices.Command cmd = result.CMD;

                int imageheight = 67;
                if (AppResource.CurrentResource.Hospital?.Logo != null)
                {
                    Image image = Image.FromStream(new System.IO.MemoryStream(AppResource.CurrentResource.Hospital?.Logo));
                    if (image.Height > imageheight)
                    {
                        double intWidth = ((double)imageheight / (double)image.Height) * (double)image.Width;
                        xrPictureBox3.Image = new Bitmap(image, new Size((int)intWidth, imageheight));
                    }

                }
                xrPictureBox1.Image = AppResource.CurrentResource.Hospital?.QRCode?.ToImage();
                txtOrgName.Text = AppResource.CurrentResource.Hospital.Name;
                labNo.Text = cmd.Code;// +" | "+result.Date.ToString("yyyy/MM/dd")+" | "+cmd.Name;
                


                StringBuilder petstr = new StringBuilder();
                petstr.Append(" | ");
                petstr.Append(cmd.Name);
                petstr.Append(" | ");
                petstr.Append(result.Date.ToString("yyyy/MM/dd"));
                petstr.Append(" | ");
                // 获取宠物信息
                RPTestPetInfo pet = logic.GetTestPet(cmd.PetId);
                if (pet == null)
                {
                    petstr.Append(""); // 病历号
                }
                else
                {
                    petstr.Append(pet.oldMedRecordNo); // 病历号
                    petstr.Append(" | ");
                }
                // petstr.Append(cmd.PetId);
                // petstr.Append(" | ");
                petstr.Append(cmd.PetName);
                petstr.Append(" | ");
                //petstr.Append(cmd.KindOf);
                //HFY添加，判断宠物品种
                rPBreedDatas = logic.GetPetBreed();
                if (pet.KindOF == "cat")
                {
                    petstr.Append("猫");
                }
                if (pet.KindOF == "dog")
                {
                    petstr.Append("犬");
                }
                if (pet.KindOF == "other")
                {
                    petstr.Append("异宠");
                }
                petstr.Append(" | ");
                petstr.Append(cmd.Gender == Devices.GenderType.Male ? "雄" : "雌");
                petstr.Append(" | ");


                DateTime pet_birth = Convert.ToDateTime(pet.birthdate);
                DateTime getnow = DateTime.Now;
                calculateTime calTime = new calculateTime();
                calTime.GetAgeByBirthday(pet_birth, getnow, out int year, out int month, out int day);
                string agestr = "";
                int ageYear = year;
                if (ageYear > 0)
                {
                    agestr = ageYear + "岁" + month + "个月" + day + "天";
                }
                else
                {
                    agestr = month + "个月" + day + "天";
                }

                /*int age = 0;
                string agestr = "";
                if (int.TryParse(cmd.Age, out age))
                {
                    if (age / 12 > 0)
                        agestr = (age / 12) + "年";
                    agestr += (age % 12) + "月";
                }*/

                petstr.Append(agestr);
                petstr.Append(" | ");
                petstr.Append(string.Format("{0:F2}", cmd.Weight) + "kg");


                txtPhysician.Text = cmd.Doctor;// labReport.exec_physician_name;


                //lblReportName.Text = cmd.Name+" | " + result.Date.ToString();
                xrTextRemark.Visible = false;
                txtPetRecordNum.Text = petstr.ToString();

                //xrPictureBox3.Image = AppResource.CurrentResource?.Hospital?.Logo?.ToImage();
                //xrPictureBox1.Image = AppResource.CurrentResource?.Hospital?.QRCode?.ToImage();

                //List<HqImgs> imgs = entity.imgs;
                //if (imgs != null && imgs.Count > 0)
                //{
                //    xrSubreportImg.ReportSource = new LabReportVirusA4Img(imgs);
                //}

                //List<HqLabReportDetails> results = entity.labvirus_details;
                // 结果分析
                if (result.Remarks != null && result.Remarks.Count > 0)
                {
                    resultText.Text = result.Remarks[0];
                }
                else {
                    resultText.Text = "无";
                }

                xrSubreportData.ReportSource = new LabReportA4Data(result);
            }

        }

    }
}
